<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 4. Variables</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="smarty.for.designers.html" title="Part II. Smarty For Template Designers">
<link rel="prev" href="language.escaping.html" title="Escaping Smarty Parsing">
<link rel="next" href="language.variable.scopes.html" title="Variable scopes">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Chapter 4. Variables</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.escaping.html">Prev</a> </td>
<th width="60%" align="center">Part II. Smarty For Template Designers</th>
<td width="20%" align="right"> <a accesskey="n" href="language.variable.scopes.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter" title="Chapter 4. Variables">
<div class="titlepage"><div><div><h2 class="title">
<a name="language.variables"></a>Chapter 4. Variables</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="language.variables.html#language.assigned.variables">Variables assigned from PHP</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="language.variables.html#language.variables.assoc.arrays">Associative arrays</a></span></dt>
<dt><span class="sect2"><a href="language.variables.html#language.variables.array.indexes">Array indexes</a></span></dt>
<dt><span class="sect2"><a href="language.variables.html#language.variables.objects">Objects</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="language.variable.scopes.html">Variable scopes</a></span></dt>
<dt><span class="sect1"><a href="language.config.variables.html">Variables loaded from config files</a></span></dt>
<dt><span class="sect1"><a href="language.variables.smarty.html">{$smarty} reserved variable</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.request">Request variables</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.now">{$smarty.now}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.const">{$smarty.const}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.capture">{$smarty.capture}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.config">{$smarty.config}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.loops">{$smarty.section}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.template">{$smarty.template}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.template_object">{$smarty.template_object}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.current_dir">{$smarty.current_dir}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.version">{$smarty.version}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.block.child">{$smarty.block.child}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.block.parent">{$smarty.block.parent}</a></span></dt>
<dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.ldelim">{$smarty.ldelim}, {$smarty.rdelim}</a></span></dt>
</dl></dd>
</dl>
</div>
<p>
  Smarty has several different types of variables. The type of the variable
  depends on what symbol it is prefixed or enclosed within.
 </p>
<p>
  Variables in Smarty can be either displayed directly or used as arguments
  for <a class="link" href="language.syntax.functions.html" title="Functions">functions</a>,
  <a class="link" href="language.syntax.attributes.html" title="Attributes">attributes</a> and
  <a class="link" href="language.modifiers.html" title="Chapter 5. Variable Modifiers">modifiers</a>, inside conditional expressions,
  etc. To print a variable, simply enclose it in the
  <a class="link" href="variable.left.delimiter.html" title="$left_delimiter">delimiters</a> so that it
  is the only thing contained between them.
</p>
<div class="example">
<a name="id346109"></a><p class="title"><b>Example 4.1. Example variables</b></p>
<div class="example-contents"><pre class="programlisting">

{$Name}

{$product.part_no} &lt;b&gt;{$product.description}&lt;/b&gt;

{$Contacts[row].Phone}

&lt;body bgcolor="{#bgcolor#}"&gt;

  </pre></div>
</div>
<p><br class="example-break">
</p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>An easy way to examine assigned Smarty variables is with the
<a class="link" href="chapter.debugging.console.html" title="Chapter 10. Debugging Console">debugging console</a>.
</p>
</div>
<p>
 </p>
<div class="sect1" title="Variables assigned from PHP">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.assigned.variables"></a>Variables assigned from PHP</h2></div></div></div>
<p>
  Assigned variables that are referenced by
  preceding them with a dollar (<code class="literal">$</code>) sign.
 </p>
<div class="example">
<a name="id346161"></a><p class="title"><b>Example 4.2. Assigned variables</b></p>
<div class="example-contents">
<p>PHP code</p>
<pre class="programlisting">

&lt;?php

$smarty = new Smarty();

$smarty-&gt;assign('firstname', 'Doug');
$smarty-&gt;assign('lastname', 'Evans');
$smarty-&gt;assign('meetingPlace', 'New York');

$smarty-&gt;display('index.tpl');

?&gt;

</pre>
<p>
      <code class="filename">index.tpl</code> source:
     </p>
<pre class="programlisting">

Hello {$firstname} {$lastname}, glad to see you can make it.
&lt;br /&gt;
{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.

   </pre>
<p>
   This above would output:
  </p>
<pre class="screen">

Hello Doug Evans, glad to see you can make it.
&lt;br /&gt;
This weeks meeting is in .
This weeks meeting is in New York.

  </pre>
</div>
</div>
<br class="example-break"><div class="sect2" title="Associative arrays">
<div class="titlepage"><div><div><h3 class="title">
<a name="language.variables.assoc.arrays"></a>Associative arrays</h3></div></div></div>
<p>
   You can also reference associative array variables by specifying the key after a dot "."
   symbol.
  </p>
<div class="example">
<a name="id346538"></a><p class="title"><b>Example 4.3. Accessing associative array variables</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;assign('Contacts',
    array('fax' =&gt; '555-222-9876',
          'email' =&gt; 'zaphod@slartibartfast.example.com',
          'phone' =&gt; array('home' =&gt; '555-444-3333',
                           'cell' =&gt; '555-111-1234')
                           )
         );
$smarty-&gt;display('index.tpl');
?&gt;

   </pre>
<p>
    <code class="filename">index.tpl</code> source:
   </p>
<pre class="programlisting">

{$Contacts.fax}&lt;br /&gt;
{$Contacts.email}&lt;br /&gt;
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}&lt;br /&gt;
{$Contacts.phone.cell}&lt;br /&gt;

   </pre>
<p>
    this will output:
   </p>
<pre class="screen">

555-222-9876&lt;br /&gt;
zaphod@slartibartfast.example.com&lt;br /&gt;
555-444-3333&lt;br /&gt;
555-111-1234&lt;br /&gt;

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Array indexes">
<div class="titlepage"><div><div><h3 class="title">
<a name="language.variables.array.indexes"></a>Array indexes</h3></div></div></div>
<p>
   You can reference arrays by their index, much like native PHP syntax.
  </p>
<div class="example">
<a name="id346598"></a><p class="title"><b>Example 4.4. Accessing arrays by index</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;assign('Contacts', array(
                           '555-222-9876',
                           'zaphod@slartibartfast.example.com',
                            array('555-444-3333',
                                  '555-111-1234')
                            ));
$smarty-&gt;display('index.tpl');
?&gt;

   </pre>
<p>
    <code class="filename">index.tpl</code> source:
   </p>
<pre class="programlisting">

{$Contacts[0]}&lt;br /&gt;
{$Contacts[1]}&lt;br /&gt;
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}&lt;br /&gt;
{$Contacts[2][1]}&lt;br /&gt;

   </pre>
<p>
    This will output:
   </p>
<pre class="screen">

555-222-9876&lt;br /&gt;
zaphod@slartibartfast.example.com&lt;br /&gt;
555-444-3333&lt;br /&gt;
555-111-1234&lt;br /&gt;

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Objects">
<div class="titlepage"><div><div><h3 class="title">
<a name="language.variables.objects"></a>Objects</h3></div></div></div>
<p>
   Properties of <a class="link" href="advanced.features.objects.html" title="Objects">objects</a>
   assigned from PHP can be referenced by specifying the property
   name after the <code class="literal">-&gt;</code> symbol.
  </p>
<div class="example">
<a name="id346667"></a><p class="title"><b>Example 4.5. Accessing object properties</b></p>
<div class="example-contents">
<pre class="programlisting">

name:  {$person-&gt;name}&lt;br /&gt;
email: {$person-&gt;email}&lt;br /&gt;

   </pre>
<p>
    this will output:
   </p>
<pre class="screen">

name:  Zaphod Beeblebrox&lt;br /&gt;
email: zaphod@slartibartfast.example.com&lt;br /&gt;

   </pre>
</div>
</div>
<br class="example-break">
</div>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.escaping.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="smarty.for.designers.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.variable.scopes.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Escaping Smarty Parsing </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Variable scopes</td>
</tr>
</table>
</div>
</body>
</html>
